package cn.hhchat.wp.entity;

import cn.hhchat.wp.dto.comment.SimpleComment;
import cn.hhchat.wp.util.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.*;
import org.hibernate.annotations.CascadeType;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;

/**
 * Created this one by HMH on 2017/10/22.
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "comment_entity")
public class CommentEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String comment;

    @ManyToOne
    @Cascade(CascadeType.SAVE_UPDATE)
    private PostEntity post;

    @ManyToOne
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    private UserEntity commenter;

    @CreationTimestamp
    private Date createTime;

    public SimpleComment toSimpleComment() {
        SimpleComment simpleComment = new SimpleComment();
        simpleComment.setContent(this.comment);
        simpleComment.setId(this.id);
        simpleComment.setUserId(this.commenter.getId());
        simpleComment.setUserName(this.commenter.getName());
        simpleComment.setCreateTime(DateUtil.formatDate(this.createTime));
        simpleComment.setPostId(this.post.getId());
        return simpleComment;
    }

}
